#!/usr/bin/env python

import sys

def main():
	if len(sys.argv) != 2:
		print('Usage: $0 on|off')
		sys.exit(1)
	
	on = sys.argv[1] == 'on'
	
	f = open('.htaccess', 'r+')
	tmp = ''
	for l in f:
		if 'bgjs_debug_false' in l:
			if on:
				tmp += '<IfDefine !bgjs_debug_false>\n'
			else:
				tmp += '<IfDefine bgjs_debug_false>\n'
		else:
			tmp += l
	f.seek(0, 0)
	f.truncate(0)
	f.write(tmp)
	f.close()
	
	f = open('bg.manifest', 'r+')
	tmp = ''
	inDebugMode = False
	inNonDebugMode = False
	for l in f:
		if inDebugMode:
			if '</debug>' in l:
				inDebugMode = False
			else:
				if not on:
					if not l.startswith('#~'):
						l = '#~' + l
				else:
					if l.startswith('#~'):
						l = l[2:]
		elif inNonDebugMode:
			if '</non-debug>' in l:
				inNonDebugMode = False
			else:
				if on:
					if not l.startswith('#~'):
						l = '#~' + l
				else:
					if l.startswith('#~'):
						l = l[2:]
		else:
			if '<debug>' in l:
				inDebugMode = True
			elif '<non-debug>' in l:
				inNonDebugMode = True
		
		tmp += l
	f.seek(0, 0)
	f.truncate(0)
	f.write(tmp)
	f.close()

if __name__ == '__main__':
	main()
